<?php

/* 
 * 分页核心model
 * @DataTime 2015-12-29
 * @author wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
*/
class pageModels extends db_help 
{

    private $table;
    private $total; //总记录数
    private $len;   //每页多少条数据
    private $limit; //生成的limit语句
    public  $page;  //第几页
    public  $pages; //总页数
    public  $uri;
    private $list;  //总共显示多少个页码数   这里必须是基数
    private $where;
    private $sort;
    private $sort_by;
    
    public $select_fields = null;

    /**
     * 为达到ajax无刷分页效果  请自行定义showpage方法
     * @param string $table 表名 无前缀的表名
     * @param array $where 条件 无需带where字样， 需要带and 或or,可数组可字符串
     * @param int $len 宽度,每页多少行数据
     * @param int $list 显示几个按扭     
     * @param string $sort 排序字段，默认是主键
     * @param string $order_by 排序方式 默认倒序
     */
    public function __construct($table, $where, $len = 3, $list = 3, $sort = '', $order_by = 'desc') {
        parent::__construct();
        $this->table_name = $this->table($table);
        $this->len   = $len;//每页多少条
        $this->where = $this->create_where_sql($where);
        $this->sort  = $sort == '' ? '' : ' order by ' . $sort . ' ' . $order_by;    
        $this->sort_by = $order_by;
        $this->total    = $this->getCount('*', $this->where);//总页数
        $this->pages    = ceil($this->total / $this->len);//总页数
        $this->list     = $list > $this->pages ? $this->pages : $list;//显示几个按钮
    }

    
    
    /**
     * 返回的结果
     * @param type $p
     * @return type
     */
    public function pageData($p = 1){
        $this->page     = ($p > $this->pages && $this->pages!=0) ? $this->pages : $p;    //当前第几页
        $this->limit    = $this->setLimit();                       //数据范围
        return array(
            'page'      =>  $this->page,
            'pages'     =>  $this->pages,
            'total'     =>  $this->total,
            'pageHtml'  =>  $this->getStri(),
            'pageData'  =>  $this->getData(),
            'sort_img'  =>  $this->sort_img(),
        );
    }
    private function sort_img(){        
        return  $this->sort_by == "DESC" ? '<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span>' : '<span class="glyphicon glyphicon-chevron-up" aria-hidden="true"></span>';
    }

    /**
     * 自定义返回结果集
     * @Author   zenghang
     * @DateTime 2015-09-15T14:34:02+0800
     * @param    integer                  $p       [description]
     * @param    array                    $display [description]
     * @return   [type]                            [description]
     */
    public function iByParam($p=1,$display = array(1,2,3,4,5,6,7,8,9,10)){
        $this->page     = $p > $this->pages ? $this->pages : $p;    //当前第几页
        $this->limit    = $this-> setLimit();                       //数据范围
        $arrAll = array(
            1       =>  array('title'=>'page','content'=>$this->page),
            2       =>  array('title'=>'pages','content'=>$this->pages),
            3       =>  array('title'=>'first','content'=>$this->first()),
            4       =>  array('title'=>'prev','content'=>$this->prev()),
            5       =>  array('title'=>'next','content'=>$this->next()),
            6       =>  array('title'=>'last','content'=>$this->last()),
            7       =>  array('title'=>'total','content'=>$this->total),
            8       =>  array('title'=>'pageHtml','content'=>$this->getStri()),//ul的分页按钮
            9       =>  array('title'=>'getStrisel','content'=>$this->getStrisel()),//select的分页按钮
            10      =>  array('title'=>'pageData','content'=>$this->getData()),//分页数据
        );
        $arrCus = array();
        foreach ($display as $k => $v) {
            $arrCus[$arrAll[$v]['title']] = $arrAll[$v]['content'];
        }
        return $arrCus;
    }
    /********************************私有方法**************************************
    /**
     * where 条件，无需带 where
     * @param type $where
     * @return string
     */
    private function create_where_sql($where) {
        if($where) {
            $where_str = ' where 1 ';
            $sql_arr = array();
            if(is_array($where)) {
                foreach ($where as $key => $value) {
                    $sql_arr[] = $key . "='{$value}'";
                }
                return $where_str . implode('and ', $sql_arr);
            } else {
                return $where_str . $where;
            }            
        }
        return '';
    }     
    /**
     * 生成数据起始范围
     */
    private function setLimit(){
        if($this->page < 1){
            $this->page = 1;
        }
        return " Limit " . ($this->page-1) * $this->len .", {$this->len}";
    }

    /**
     * 生成的分页字符串
     */
    private function getStri(){
        $pageHtml = '';
        if ($this->pages > 1) {
            $pageHtml .= "<li><a href='javascript:void(0);' onclick='javascript:listTable.showPage(1)' class='pagebtn'>首页</a></li>";
            $pageHtml .= "<li><a href='javascript:void(0);' onclick='javascript:listTable.showPage(" . ($this->page-1 <=0 ? $this->pages : $this->page-1) . ")' class='pagebtn'>上一页</a></li>";
            if ($this->page < ceil($this->list / 2)) {
                $left   = 0;
                if ($this->list >= $this->pages) {
                    $right  = $this->pages;
                } else {
                    $right  = $this->list;
                }
            } elseif ($this->page + ceil($this->list/2) > $this->pages){
                $left   = $this->pages - $this->list + 1;
                $right  = $this->pages;
            } else {
                $left   = $this->page - floor($this->list/2);
                $right  = $this->page + floor($this->list/2);
            }
            for ($i = $left; $i <= $right; $i++) {
                if ($i > 0) {
                    if ($i == $this->page) {
                        $pageHtml.="<li class='active'><a href='javascript:void(0);'>$i</a></li>";//这里添加高亮样式
                    } else {
                        $pageHtml.="<li><a href='javascript:void(0);' onclick='listTable.showPage($i)'>$i</a></li>";
                    }
                }   
            } 
            $pageHtml .= "<li><a href='javascript:void(0);' onclick='javascript:listTable.showPage(" . ($this->page+1 >$this->pages ? 1 : $this->page+1) . ")' class='pagebtn'>下一页</a></li>";
            $pageHtml .= "<li><a href='javascript:void(0);' onclick='javascript:listTable.showPage($this->pages)' class='pagebtn'>尾页</a></li>";
        }
        if ($pageHtml != '') {
            return '<ul>' . $pageHtml . '</ul>';
        } else {
            return $pageHtml;
        }
    }

    /**
     * [getStri description]
     * @Author   zenghang
     * @DateTime 2015-09-15T14:17:26+0800
     * @return   [type]                   [description]
     */
    private function getStrisel(){
        $pageHtml = '';
        if ($this->pages > 1) {
            if ($this->page< ceil($this->list/2)) {
                $left   = 0;
                if ($this->list >= $this->pages) {
                    $right  = $this->pages;
                } else {
                    $right  = $this->list;
                }
            } elseif ($this->page + ceil($this->list/2) > $this->pages){
                $left   = $this->pages-$this->list+1;
                $right  = $this->pages;
            } else {
                $left   = $this->page-floor($this->list/2);
                $right  = $this->page+floor($this->list/2);
            }
            for ($i = $left; $i <= $right; $i++) {
                if ($i>0) {
                    if ($i==$this->page) {
                        $pageHtml.="<option selected='selected'>{$i}</option>";//这里添加高亮样式
                    } else {
                        $pageHtml.="<option>{$i}</option>";
                    }
                }   
            } 
        }
        if ($pageHtml!='') {
            return '<select onchange="showpage(this.options[this.options.selectedIndex].value)">'.$pageHtml.'</select>';
        } else {
            return $pageHtml;
        }
    }

    /**
     * 首页
     * @Author   zenghang
     * @DateTime 2015-09-15T14:23:01+0800
     * @return   [type]                   [description]
     */
    private function first(){
        if ($this->pages > 1) {
            return "<a href='javascript:void(0);' onclick='javascript:showpage(1)' class='btn-f'>首页</a>";
        } else {
            return '';
        }
    }

    /**
     * 尾页
     * @Author   zenghang
     * @DateTime 2015-09-15T14:24:36+0800
     * @return   [type]                   [description]
     */
    private function last(){
        if ($this->pages > 1) {
            return "<a href='javascript:void(0);' onclick='javascript:showpage($this->pages)' class='btn-l'>尾页</a>";
        } else {
            return '';
        }
    }

    /**
     * 上一页
     * @Author   zenghang
     * @DateTime 2015-09-15T14:25:01+0800
     * @return   [type]                   [description]
     */
    private function prev(){
        if ($this->pages > 1) {
            return "<a href='javascript:void(0);' onclick='javascript:showpage(" . ($this->page-1 <=0 ? $this->pages : $this->page-1) . ")' class='btn-prev'>上一页</a>";
        } else {
            return '';
        }
    }

    /**
     * 下一页
     * @Author   zenghang
     * @DateTime 2015-09-15T14:26:08+0800
     * @return   function                 [description]
     */
    private function next(){
        if ($this->pages > 1) {
            return "<a href='javascript:void(0);' onclick='javascript:showpage(" . ($this->page+1 >$this->pages ? 1 : $this->page+1) . ")' class='btn-next'>下一页</a>";
        } else {
            return '';
        }
    }

    /*分页后的数据*/
    private function getData(){     
        $select_fields = '*';
        if($this->select_fields) {
            $select_fields = $this->select_fields;
        }
        $sql = "select {$select_fields} from " . $this->table_name . $this->where . $this->sort . $this->limit;
        return $this->result_array($sql);
    }
}

